package com.rightsidetech.http.controller.admin.system;

import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.enums.ModifyName;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.ModifyLog;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.SysRoleAddReq;
import com.rightsidetech.system.dtos.request.SysRoleReq;
import com.rightsidetech.system.dtos.request.SysRoleUpdateReq;
import com.rightsidetech.system.dtos.response.SysRoleDetailResp;
import com.rightsidetech.system.dtos.response.SysRoleResp;
import com.rightsidetech.system.service.SysRoleService;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

/**
 * @Author: yhr
 * @Date: 2021-10-20 09:07
 */
@RestController
@RequestMapping("/admin/sys_role")
@ShenyuSpringMvcClient(path = "/admin/sys_role/**")
@Api(tags = "admin:后台角色")
public class SysRoleController extends BaseController {

    @Resource
    private SysRoleService sysRoleService;

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    @ApiOperation("admin:分页查询角色信息")
    public ResponseData<PageInfo<SysRoleResp>> page(SysRoleReq sysRoleReq) throws Exception {
        return sysRoleService.page(sysRoleReq);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/add")
    @ApiOperation("admin:添加角色")
    @ModifyLog(serviceClass = SysRoleService.class,modifyType = ModifyName.SAVE)
    public ResponseData saveSysRole(@RequestBody SysRoleAddReq sysRoleAddReq) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(sysRoleAddReq);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysRoleService.saveSysRole(sysRoleAddReq,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/update")
    @ApiOperation("admin:修改角色")
    @ModifyLog(serviceClass = SysRoleService.class)
    public ResponseData updateSysRole(@RequestBody SysRoleUpdateReq sysRoleUpdateReq) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(sysRoleUpdateReq);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysRoleService.updateSysRole(sysRoleUpdateReq,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/del")
    @ApiOperation("admin:删除角色")
    @ModifyLog(serviceClass = SysRoleService.class)
    public ResponseData delSysRole(@RequestBody DelReq delReq) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(delReq);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return sysRoleService.delSysRole(delReq);
    }

    @Log
    @Token
    @GetMapping("/search")
    @ApiOperation("admin:角色下拉框加载数据")
    public ResponseData selectList() throws Exception {
        return sysRoleService.selectList();
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/getRoleDetail")
    @ApiOperation("admin:角色详情")
    public ResponseData<SysRoleDetailResp> getRoleDetail(Long roleId) throws Exception {
        return sysRoleService.getRoleDetail(roleId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/getResourcesByRoleId")
    @ApiOperation("admin:根据角色id查询菜单")
    public ResponseData getResources(Long roleId) throws Exception {
        return sysRoleService.getResources(roleId);
    }


    @Log
    //@Token
    @GetMapping("/bindRoles")
    @ApiOperation("一键添加管理员帐号、角色、密码 并绑定所有菜单权限(测试专用)")
    public ResponseData bindRolesByLoginName(String loginName,String roleName, String pwd) throws Exception{
        //Long loginInfoId = Long.parseLong(getAttribute());
        return sysRoleService.bindRoles(loginName, roleName, pwd);
    }

}
